﻿#define _CRT_SECURE_NO_WARNINGS 1 //安心，加油！

#include<iostream>
#include<map>
using namespace std;


//1.map基本用法(跟set类似)，map跟set区别在于
//set为一种有序、去重的集合，而map是一种有序的字典或键值对集合
int main()
{
	//2.pair类型介绍
	//map每个元素都是一个pair<Key,T>类型数据

	map<string, string> dict1 = { {"left","左边"},{"right","右边"} ,{"insert","插入"} ,{"string","字符串"} };

	pair<string, string> kv1{ "first","1" };
	map<string, string> dict2{ kv1,pair<string,string>{"second","2"} };

	map<string,string>::iterator it = dict1.begin();
	while (it != dict1.end())
	{
		cout << it->first << ":" << it->second << endl;
		it++;
	}
	cout << endl;
	//insert:插入
	//left : 左边
	//right : 右边
	//string : 字符串

	for (const auto& e : dict2)
	{
		cout << e.first << ":" << e.second << endl;
	}
	cout << endl;
	//first:1
	//second : 2
	


	//3.增删查改
	dict2.insert({ {"third","3"},{"fourth","4"} });
	dict2.insert({ {"third","7"},{"fourth","4"}, {"fifth","4"} });//<Key,Value>Key不相同才会插入
	auto it2 = dict2.begin();
	while (it2 != dict2.end())
	{
		it2->second += "1";//Key不可修改，Value可修改
		cout << it2->first << ":" << it2->second << endl;
		it2++;
	}
	cout << endl;
	//fifth:41
	//first : 11
	//fourth : 41
	//second : 21
	//third : 31
	
	dict2.erase({ "third" });//检测Key
	for (const auto& e : dict2)
	{
		cout << e.first << ":" << e.second << endl;
	}
	cout << endl;
	//fifth:41
	//first : 11
	//fourth : 41
	//second : 21

	string arr[] = { "苹果", "西瓜", "苹果", "西瓜", "苹果", "苹果", "西瓜","苹果", "香蕉", "苹果", "香蕉" };
	map<string, int> countMap;
	for (const auto& str : arr)
	{
		//法1
		countMap[str]++;

		////法2
		//auto ret = countMap.find(str);//没找到，ret指向end()
		//if (ret == countMap.end())
		//{
		//	countMap.insert({ str,1 });
		//}
		//else
		//{
		//	ret->second++;
		//}

	}
	for (const auto& e : countMap)
	{
		cout << e.first << ":" << e.second << endl;
	}
	cout << endl;
	//苹果:6
	//西瓜 : 3
	//香蕉 : 2

	dict2.insert(make_pair("安心", "50"));//make_pair自动识别类型
	for (const auto& e : dict2)
	{
		cout << e.first << ":" << e.second << endl;
	}
	cout << "                  " << dict2["first"] << endl;//value
	dict2["安心"] = "52";//修改
	for (const auto& e : dict2)
	{
		cout << e.first << ":" << e.second << endl;
	}
	cout << endl;



	//4.multimap和map
	multimap<string, string> dict3;
	dict3.insert({"first","1"});
	dict3.insert({"third","3"});//全部插入，不去重，也会排列
	dict3.insert({"third","3"});
	dict3.insert({"first","2"});
	dict3.insert({"second","2"});
	for (const auto& e : dict3)
	{
		cout << e.first << ":" << e.second << endl;
	}
	//first:1
	//first : 2
	//second : 2
	//third : 3
	//third : 3


	return 0;
}








